var express = require('express');
const Mongoose = require('mongoose');
const { ObjectId } = Mongoose.Types;
var router = express.Router();
var articleModel = require('../tool/model/article.js');
var usertModel = require('../tool/model/clientUser.js');
router.post('/', (req, res, next)=> {
  var obj = req.body;
  const {pageNum = 1, pageSize = 5} = obj
  if(obj.kinds){
    console.log(obj)
  articleModel.aggregate([
    {
      $lookup:
        {
          from: "comment",
          localField: "_id",
          foreignField: 'commmentArticleId',
          as: "comments"
        }
   },{
    $match:{'status':{'$eq':1},'kind':{'$eq':parseInt( obj.kinds)}}
   }
  ]).skip((pageNum - 1) * pageSize)
  .limit(pageSize)
  .sort({'ctime': -1})
  .exec(function(err,docs){
    if(err){
      console.log(err);
      return;
  }
    console.log(docs)
    res.send(docs)
  })
  }else{
    console.log(obj)
      articleModel.aggregate([
        {
          $lookup:
            {
              from: "comment",
              localField: "_id",
              foreignField: 'commmentArticleId',
              as: "comments"
            }
      },{
        $match:{'status':{'$eq':1}}
      }]).skip((pageNum - 1) * pageSize)
      .limit(pageSize)
      .sort({'ctime': -1})
      .exec(function(err,docs){
        if(err){
          console.log(err);
          return;
      }
        console.log(docs)
        res.send(docs)
      })
      
  }
  
});
router.post('/article', (req, res, next)=> {
  let obj = req.body
  console.log(obj)
    articleModel.aggregate([
      {
        $lookup:
          {
            from: "comment",
            localField: "_id",
            foreignField: "commmentArticleId",
            as: "comments"
          }
     },
     {
      $lookup:
        {
          from: "clientuser",
          localField: "articleMark",
          foreignField: "userID",
          as: "info"
        }
   }, 
   {
    $lookup:
      {
        from: "user",
        localField: "articleMark",
        foreignField: "userID",
        as: "infos"
      }
 },
    {
      $match:{'status':{'$eq' :1 },'_id':{'$eq': new ObjectId(obj.id)}} 
     }],function(err,docs){
      if(err){
        console.log(err);
        return;
    }
      console.log(docs)
      articleModel.update({ _id: new ObjectId(obj.id) }, 
        { $set: req.body, $inc: { scanned: 1 } }, 
        { multi: false },  function(err,data){
          if(err){
            console.log(err);
            return;
          }
        });
        res.send({
          status: '1',
          type:'success',
          content: docs,
          result: 'success'
         })
    })

});
router.post('/updatearticle', (req, res, next)=> {
  let obj = req.body
  articleModel.find({_id: new ObjectId(obj.id),wholikes:obj.userId},function(err,docs){
      if(docs.length == 0){
        articleModel.update({ _id: new ObjectId(obj.id) }, 
        { $set: req.body, $inc: { likes: 1 } ,'wholikes':obj.userId}, 
        { multi: false },  function(err,data){
          if(err){
            res.send({
              status: '0',
              type:'error',
              content: '点赞失败！',
              result: 'error'
             })
          }
          res.send({
            status: '1',
            type:'success',
            content: '点赞成功！',
            result: 'success'
           })
        });
      }else{
        res.send({
          status: '3',
          type:'error',
          content: '你已经点过赞了！还想怎样！！',
          result: 'success'
         })
      }
  })
});
router.post('/count',function(req, res, next) {
  let data = req.body; 
  console.log(data)
  usertModel.aggregate([
    {
      $lookup:{
        from: "article",
        localField: "userID",
        foreignField: "articleMark",
        as: "counts"
      }
    }, 
    {
      $match:
      {
          'userID': {'$eq':data.userId}
      }
  },
  ],function(err,docs){
    res.send({
      status: '1',
      type:'success',
      data:docs,
      result: 'success'
     })
  })
});
router.post('/articledel',function(req, res, next) {
  let data = req.body; 
      status=data.status,
      id=data.id,
  console.log(data)
   articleModel.update({
     _id: data.id}, 
     {'status':status},(err,docs)=>{
    if (err) {
      res.send({
          status: '1',
          type:'error',
          message: error.message,
          result: 'err'
      })
  } else {
      res.send({
          status: '0',
          type:'success',
          message: '更改成功！',
          result: 'suc'
      })
    }
   })
});
module.exports = router;